// /**
//  * @description
//  * 根据给出的例子，实现 Person
//  * @example
//  * Person("Li");
//  * 输出： Hi! This is Li!
//  * Person("Dan").sleep(10).eat("dinner");
//  * 输出：
//  * Hi! This is Dan!
//  * 等待10秒..
//  * Wake up after 10
//  * Eat dinner~
//  * Person("Jerry").eat("dinner").eat("supper");
//  * 输出：
//  * Hi This is Jerry!
//  * Eat dinner~
//  * Eat supper~
//  * Person("Smith").sleepFirst(5).eat("supper");
//  * 输出：
//  * 等待5秒
//  * Wake up after 5
//  * Hi This is Smith!
//  * Eat supper
//  */

// 先要有一个对象进行保存this

function Person(name) {
  //任务队列
  var arr = new Array();

  var obj = {
    next() {
      // console.log(arr);

      var fn = arr.shift();
      if (fn != undefined) fn();
    },
    clgName(name) {
      arr.push(() => {
        console.log("Hi! This is " + name + "!");
        this.next();
      });
      return this;
    },
    sleep(time) {
      var that = this;
      arr.push(function () {
        setTimeout(function () {
          console.log(`get up after ${time}`);
          that.next();
        }, time * 1000);
      });
      return this;
    },
    sleepFirst(time) {
      var that = this;
      arr.unshift(function () {
        setTimeout(function () {
          console.log(`get up after ${time}`);
          that.next();
        }, time * 1000);
      });
      return this;
    },
    eat(what) {
      var that = this;
      arr.push(function () {
        console.log(`Eat ${what} ~`);
        that.next();
        // this.next()
      });
      return this;
    },
  };

  //任务全部放入队列
  obj.clgName(name);

  // 全部队列放入 开始执行
  setTimeout(function () {
    obj.next();
  });
  return obj;
}

// Person("lwt").sleep(2).eat('meal');



console.log('----------------------------------------------------------------');

//send
/**
 * @description
 * 根据给出的例子，实现 Person
 * @example

 * Person("Li");
 * 输出： Hi! This is Li!

 * Person("Dan").sleep(10).eat("dinner");
 * 输出：
 * Hi! This is Dan!
 * 等待10秒..
 * Wake up after 10
 * Eat dinner~

 * Person("Jerry").eat("dinner").eat("supper");
 * 输出：
 * Hi This is Jerry!
 * Eat dinner~
 * Eat supper~

 * Person("Smith").sleepFirst(5).eat("supper");
 * 输出：
 * 等待5秒
 * Wake up after 5
 * Hi This is Smith!
 * Eat supper
 */

function Person(name) {
    //任务队列
    var arr = new Array();
  
    var obj = {
      next() {
          // console.log(arr);
          
        var fn = arr.shift();
        if ( fn != undefined) fn();
      },
      clgName(name) {
        arr.push(() => {
          console.log("Hi! This is " + name + "!");
          this.next()
        });
        return this;
      },
      sleep(time) {
        var that = this;
        arr.push(function () {
          setTimeout(function () {
            console.log(`get up after ${time}`);
            that.next();
          }, time * 1000);
        });
        return this;
      },
      sleepFirst(time) {
          var that = this;
          arr.unshift(function () {
            setTimeout(function () {
              console.log(`get up after ${time}`);
              that.next();
            }, time * 1000);
          });
          return this;
        },
      eat(what){ var that = this;
          arr.push(function(){
              console.log(`Eat ${what} ~`);
              that.next()            
              // this.next()
          })
          return this
      }
    };
  
    //任务全部放入队列
    obj.clgName(name);
  
    // 全部队列放入 开始执行
    setTimeout(function(){
        obj.next()
    })
    return obj;
  }
  
  export { Person };
  